package railRoad.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import railRoad.User;
import railRoad.dao.interfaces.IDAO;
import railRoad.db.DBConectionPool;
import railRoad.exception.DBException;

/**
 * Implementation of {@link#IDAO} for {@link#User}
 * 
 * @see IDAO
 * @see User
 * @author Rukin
 *
 */
public class UserDAO implements IDAO<User> {

	DBConectionPool conectionPool = DBConectionPool.getInstance();

	public User select(int id) throws DBException {

		User user = null;
		Connection connection = null;
		try {
			connection = conectionPool.getConnection();
			connection.setAutoCommit(false);

			Statement statement = connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT * FROM SYSTEMUSER WHERE USER_ID = "
							+ id);

			while (resultSet.next()) {
				int uId = resultSet.getInt("USER_ID");
				int hId = resultSet.getInt("PASSANGER_ID");
				String login = resultSet.getString("LOGIN");
				String pass = resultSet.getString("PASSWORD");
				String email = resultSet.getString("EMAIL");
				boolean admin = resultSet.getBoolean("admin");

				user = new User(uId, login, pass,email, hId, admin);
			}
		} catch (SQLException e) {
			throw new DBException(e.getMessage());
		} finally {
			conectionPool.close(connection);
		}// finally

		return user;
	}

	public User select(String field) throws DBException {
		User user = null;
		Connection connection = null;
		try {
			connection = conectionPool.getConnection();
			connection.setAutoCommit(false);

			Statement statement = connection.createStatement();
			ResultSet resultSet = statement
					.executeQuery("SELECT * FROM SYSTEMUSER WHERE LOGIN = '"
							+ field + "'");

			while (resultSet.next()) {
				int uId = resultSet.getInt("USER_ID");
				int hId = resultSet.getInt("PASSANGER_ID");
				String login = resultSet.getString("LOGIN");
				String pass = resultSet.getString("PASSWORD");
				String email = resultSet.getString("EMAIL");
				boolean admin = resultSet.getBoolean("admin");

				user = new User(uId, login, pass,email, hId, admin);
			}
		} catch (SQLException e) {
			throw new DBException(e.getMessage());
		} finally {
			conectionPool.close(connection);
		}// finally

		return user;
	}

	public int insert(User user, int parrentId) throws DBException {
		Connection connection = null;
		int rezult = -1;
		try {
			connection = conectionPool.getConnection();
			connection.setAutoCommit(false);

			PreparedStatement preparedStatement = connection
					.prepareStatement("INSERT INTO SYSTEMUSER( LOGIN , PASSWORD , PASSANGER_ID , ADMIN, EMAIL ) VALUES (?,?,?,?,?)");

			preparedStatement.setString(1, user.getLogin());
			preparedStatement.setString(2, user.getPassword());
			preparedStatement.setInt(3, user.getHuman_id());
			preparedStatement.setBoolean(4, user.isAdmin());
			preparedStatement.setString(5, user.getEmail());
			
			rezult = preparedStatement.executeUpdate();
			
			ResultSet resultSet = connection.createStatement().executeQuery("SELECT MAX(USER_ID) as ID FROM SYSTEMUSER");
			int id = -1;
			while(resultSet.next())
			{
				id = resultSet.getInt("ID");
			}
			
			user.setUser_id(id);
			
			connection.commit();

		} catch (SQLException e) {
			throw new DBException(e.getMessage());
		} finally {
			conectionPool.close(connection);
		}// finally

		return rezult;
	}//insert

	public int update(User updatedObject) throws DBException {
		return -1;
	}

	public int delete(User removedObject) throws DBException {
		return -1;
	}

}
